package cn.nyse.ktc_user.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import wfw.cn.ktc_common.unit.IdWorker;
import cn.nyse.ktc_user.dao.FollowDao;
import cn.nyse.ktc_user.entity.Follow;

/**
 * @Description tb_follow 服务层
 * @author admin
 * @date 2020-10-14 17:36:57
*/
@Service
public class FollowServiceImpl implements FollowService{

	@Autowired
	private FollowDao followDao;

	@Autowired
	private IdWorker idWorker;

	/**
	* 查询全部列表
	* @return
	*/
	public List<Follow> findAll() {
		return followDao.findAll();
	}

	/**
	* 条件查询+分页
	* @param whereMap
	* @param page
	* @param size
	* @return
	*/
	public Page<Follow> findSearch(Map whereMap, int page, int size) {
		Specification<Follow> specification = createSpecification(whereMap);
		PageRequest pageRequest =  PageRequest.of(page-1, size);
		return followDao.findAll(specification, pageRequest);
	}

	/**
 	* 条件查询
	* @param whereMap
	* @return
	*/
	public List<Follow> findSearch(Map whereMap) {
		Specification<Follow> specification = createSpecification(whereMap);
		return followDao.findAll(specification);
	}

	/**
	* 根据ID查询实体
	* @param id
	* @return
	*/
	public Follow findById(String id) {
		return followDao.findById(id).get();
	}

	/**
	* 增加
	* @param follow
	*/
	public void add(Follow follow) {
//		follow.setId( idWorker.nextId()+"" );
		followDao.save(follow);
	}

	/**
	* 修改
	* @param follow
	*/
	public void update(Follow follow) {
		followDao.save(follow);
	}

	/**
	* 删除
	* @param id
	*/
	public void deleteById(String id) {
		followDao.deleteById(id);
	}

	/**
	* 动态条件构建
	* @param searchMap
	* @return
	*/
	private Specification<Follow> createSpecification(Map searchMap) {

		return new Specification<Follow>() {

			@Override
			public Predicate toPredicate(Root<Follow> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> predicateList = new ArrayList<Predicate>();

				return cb.and( predicateList.toArray(new Predicate[predicateList.size()]));
			}
		};
	}

}
